<?php

namespace App\Service\delivery;
use App\Model\Applet;
use App\Model\AppletCompany;
use App\Model\Company;
use App\Model\DataSpreadPlatform;
use App\Service\CommicService;
use App\Model\DataBackhaulSetting;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
class DataBackhaulSettingService extends CommicService
{
    const cacheDuration = 60 * 15;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
        $this->userAppletIds = $this->getUserAppletInfo();
    }


    const STATUS_ENABLE  = 1;


    public function search(Array $params){

        $keyword     = $params['keyword'] ?? '';
//        $company_id  = $params['company_id'] ?? 0;
//        $applet_id   = $params['applet_id'] ?? 0;
        $pageSize    = $params['pageSize'] ?? 10;
        $page        = $params['page'] ?? 1;
        $applet_ids     = $params['applet_ids'] ?? '';
        if(empty($applet_ids)){
            return [];
        }
        //获取所拥有的
        if($applet_ids == '1'){
            //获取所有的小程序所绑定的短剧
            $applet_id =  $this->userAppletIds;
        }else{
            $applet_id =  [$applet_ids];
        }

        $query = DataBackhaulSetting::query()->select(['id','name','spread_platform_id','company_id','applet_id','created_at','updated_at']);

        if ($keyword) {
            $query->orWhere([['name', 'like', '%'.$keyword.'%']]);
        }
//        if(!empty($company_id)){
//            $query->where(['company_id' => $company_id]);
//        }
        if(!empty($applet_id)){
            $query->whereIn('applet_id',$applet_id);
        }
        //根据用户获取ID
        $userInfo = $this->getUserAppletInfo(2);
        if($userInfo['role']['is_admin'] != 1){
            $query->where('user_id',$userInfo['id']);
        }

        $models = $query->orderBy('updated_at','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();
            $data['spread_platform_name']  = '';
            if(!empty($item->platform)) {
                $data['spread_platform_name'] = $item->platform->name;
            }

            if(!empty($item->applet)){
                $data['applet_name']  = $item->applet->name;
            }else{
                $data['applet_name']  = '';
            }
            if(!empty($item->company)){
                $data['company_name']  = $item->company->name;
            }else{
                $data['company_name']  = '';
            }

            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }

    public function createOrUpdate(Array $postData,bool $is_update = false,DataBackhaulSetting $dataBackhaulSettingInfo = null){
        $name = $postData['name'];
        $applet_id = $postData['applet_id'];
        //$company_id = $postData['company_id'];
        $spread_platform_id = $postData['spread_platform_id'] ?? 1;
        $ad_type = $postData['ad_type'] ?? 1;
        $report_method = $postData['report_method'] ?? 1;
        $user_activation_report = $postData['user_activation_report'] ?? 1;
        $report_type = $postData['report_type'] ?? 1;
//        $order_property = $postData['order_property'] ?? 1;
//        $order_pay_type = $postData['order_pay_type'] ?? 1;
        $backhaul_type = $postData['backhaul_type'] ?? 1;
        $minimun_report_amount = $postData['minimun_report_amount'] ?? 1;
        $backhaul_filterate_num= $postData['backhaul_filterate_num'] ?? 0;
        $backhaul_filterate_miss = $postData['backhaul_filterate_miss'] ?? 0;
        $start_protect = $postData['start_protect'] ?? 0;

        //根据用户获取ID
        $userInfo = $this->getUserAppletInfo(2);
        $company_id = $userInfo['company_id'];


        $model = !empty($dataBackhaulSettingInfo)? $dataBackhaulSettingInfo : new DataBackhaulSetting();


        //查询投放平台是否存在
        $dataSpreadPlatformData = DataSpreadPlatform::query()->where(['id' => $spread_platform_id])->exists();
        if(empty($dataSpreadPlatformData)){
            return $this->response(SystemCode::SYSTEM_ERROR, '投放平台不存在');
        }
        //查看公司是否存在
        $companyData = Company::query()->where(['id' => $company_id])->exists();
        if(empty($companyData)){
            return $this->response(SystemCode::SYSTEM_ERROR, '公司不存在');
        }
        //查找小程序是否存在
        $appletData = Applet::query()->where(['id' => $applet_id])->exists();
        if(empty($appletData)){
            return $this->response(SystemCode::SYSTEM_ERROR, '请选择小程序');
        }
        //查看小程序和公司是否存在绑定关系
        $appletCompanyData = AppletCompany::query()->where(['company_id' => $company_id,'applet_id' => $applet_id])->exists();
        if(empty($appletCompanyData)){
            return $this->response(SystemCode::SYSTEM_ERROR, '公司和小程序不存在绑定关系小程序');
        }
        $model->user_id = $userInfo['id'];
        $model->name = $name;
        $model->company_id = $company_id;
        $model->applet_id = $applet_id;
        $model->spread_platform_id = $spread_platform_id;
        $model->ad_type = $ad_type;
        $model->report_method = $report_method;
        $model->user_activation_report = $user_activation_report;
        $model->report_type = $report_type;
        if($report_type == 2){
            $backhaul_type = 1;
            $order_property = 1;
            $order_pay_type = 1;
            $minimun_report_amount = 0;
            $backhaul_filterate_num = 0;
            $backhaul_filterate_miss = 0;
        }
        $model->backhaul_type = $backhaul_type;
//        $model->order_property = $order_property;
//        $model->order_pay_type = $order_pay_type;
        $model->minimun_report_amount = $minimun_report_amount;
        $model->backhaul_filterate_num = $backhaul_filterate_num;
        $model->backhaul_filterate_miss = $backhaul_filterate_miss;
        $model->start_protect= $start_protect;

        if($model->save()){
            return $this->response(SystemCode::SYSTEM_SUCCESS);
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR,'保存错误');
        }
    }
}